// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html


const { ccclass, property } = cc._decorator;

@ccclass
export default class BaseManager {
    public static _s_instance: BaseManager | null = null;
    public static getInstance(): BaseManager {
        if (!this._s_instance)
            this._s_instance = new BaseManager();

        return this._s_instance;
    }

    tt: any = null;

    is_cebianlan_use: boolean = true;//是否可以使用侧边栏
    is_cebianlan: boolean = false;//是否从侧边栏返回
    is_cebianlan_jiangli: boolean = false;//是否领取奖励
    public initNavigate() {
        this.tt = window["tt"] || undefined;

        this.is_cebianlan_jiangli = (localStorage.getItem("is_cebianlan_jiangli")) ? JSON.parse(localStorage.getItem("is_cebianlan_jiangli")) : false;

        this.onShowNavigate();

    }

    /**
     * 收藏桌面
     * @returns 
     */
    public showShouCang() {
        if (this.tt == undefined)
            return;

        this.tt.showFavoriteGuide({
            type: "bar",
            content: "一键添加到我的小程序",
            position: "bottom",
            success(res) {
                console.log("引导组件展示成功");
            },
            fail(res) {
                console.log("引导组件展示失败");
            },
        });
    }
    isclick: boolean = false;
    //分享按钮（口令）
    ShareEvent() {
        if (this.tt == null)
            return;
        if (this.isclick == false) {
            this.isclick = true;

            setTimeout(() => {
                this.isclick = false
            }, 200);
            this.tt.shareAppMessage({
                channel: "",
                //title: "分享视频",
                desc: "",
                templateId: "", // 替换成通过审核的分享ID
                query: "",
                success() {
                    console.log("分享成功");
                },
                fail(e) {
                    console.log("分享失败");
                },
            });
        }

    }

    onShowNavigate() {
        if (this.tt == null)
            return;

        this.tt.onShow((res) => {
            console.log("启动参数：", res.query);
            console.log("来源信息：", res.refererInfo);
            console.log("场景值：", res.scene);
            console.log("启动场景字段：", res.launch_from, ", ", res.location);

            if (res.launch_from == "homepage" && res.location == "sidebar_card") {
                this.is_cebianlan = true;
                
            } else {
                this.is_cebianlan = false;
            }
        });

        this.tt.checkScene({
            scene: "sidebar",
            success: (res) => {
                console.log("check scene success: ", res.isExist);
                //成功回调逻辑
                this.is_cebianlan_use = true;
            },
            fail: (res) => {
                console.log("check scene fail:", res);
                //失败回调逻辑
                this.is_cebianlan_use = false;
            }
        });

    }

    navigateToScene() {
        this.tt = window["tt"] || undefined;
        console.log("准备进入侧边栏！！！");
        if (this.tt == null)
            return;
        console.log("准备进入侧边栏");
        this.tt.navigateToScene({
            scene: "sidebar",
            success: (res) => {
                console.log("navigate to scene success");
                // 跳转成功回调逻辑
            },
            fail: (res) => {
                console.log("navigate to scene fail: ", res);
                // 跳转失败回调逻辑
            },
        });

    }

    recorder: any = null;//录屏管理
    _videoPath: any = null;//视频路径
    private _isRecord: boolean = false;//是否已经开始录屏
    private _isRecordSuccess: boolean = false;//是否录屏超过3秒
    private _isRecordEnd: boolean = false;

    init_record() {
        if (this.tt == undefined)
            return;
        if (!this.recorder) {
            this.recorder = this.tt.getGameRecorderManager();
            this.recorder.onStart((res) => {
                console.log("录屏开始");
            })

            this.recorder.onStop(res => {
                this._videoPath = res.videoPath;
                console.log("录屏结束");
            })
        };
    }
    /**
     * 开始录屏
     * @returns 
     */
    public addRecord() {
        if (this.tt == null)
            return;

        this.recorder.stop();
        this.recorder.start({
            duration: 120,
        });
        this._isRecord = true;
        this._isRecordEnd = false;
        this._isRecordSuccess = false;
        setTimeout(() => {
            if (!this._isRecordEnd)
                this._isRecordSuccess = true;
        }, 3000);
    }
    /**
     * 停止录屏
     * @returns 
     */
    public addRecordEnd(pBool: boolean = true) {
        if (this.tt == null)
            return;

        if (this._isRecordEnd)
            return;

        this._isRecord = false;
        this._isRecordEnd = true;
        setTimeout(() => {
            this._isRecordEnd = false;
        }, 500);

        this.recorder.stop();
        this._isRecord = false;
        if (pBool) {
            if (this._isRecordSuccess) {
                this.addShareRecord()
            }
            else {
                console.log("录屏时间小于3秒");
                
            }
        }
    }
    /**
     * 分享录屏
     * @returns 
     */
    public addShareRecord() {
        if (this.tt == null)
            return;

        setTimeout(() => {
            this.tt.shareAppMessage({
                channel: 'video',
                title: '分享视频',
                imageUrl: '',
                query: '',
                extra: {
                    videoPath: this._videoPath, // 可用录屏得到的视频地址
                    //videoTopics: ['话题']
                },
                success() {
                    console.log('分享视频成功');
                },
                fail(e) {
                    console.log('分享视频失败');
                }
            });
        }, 500);
    }

    /**
     * 更多游戏
     * @returns 
     */
    public onMoreGames() {
        this.tt = window["tt"] || undefined;
        if (this.tt == null)
            return;

        // iOS 不支持，建议先检测再使用
        if (this.tt.getSystemInfoSync().platform !== "ios") {
            // 打开互跳弹窗
            this.tt.showMoreGamesModal({
                appLaunchOptions: [],
                success(res) {
                    console.log("success", res.errMsg);
                },
                fail(res) {
                    console.log("fail", res.errMsg);
                },
            });
        } else {
            this.tt.showToast({
                title: " iOS不支持此功能",
            });
        }
    }

    /**
     * 一键收藏
     * @returns 
     */
    public onShouCang() {
        this.tt = window["tt"] || undefined;
        if (this.tt == null)
            return;

        this.tt.showFavoriteGuide({
            type: "bar",
            content: "一键添加到我的小程序",
            position: "bottom",
            success(res) {
                console.log("success", res.errMsg);
            },
            fail(res) {
                console.log("fail", res.errMsg);
            },
        });
    }

    /**
    * 短震动 30ms
    * @returns 
    */
    public vibrateShort() {
        this.tt = window["tt"] || undefined;
        if (this.tt == null)
            return;

        this.tt.vibrateShort({
            success(res) {
                console.log(`${res}`);
            },
            fail(res) {
                console.log(`vibrateShort调用失败`);
            },
        });
    }
    /**
     * 长震动400 ms
     * @returns 
     */
    public vibrateLong() {
        this.tt = window["tt"] || undefined;
        if (this.tt == null)
            return;
        this.tt.vibrateLong({
            success(res) {
                console.log(res);
            },
            fail(res) {
                console.log(`vibrateLong调用失败`);
            },
        });
    }
}
